package ru.xeden.web.html.component;

import java.util.Map;

import ru.xeden.core.common.ObjectRegistr;
import ru.xeden.web.html.dom.Element;


/**
 * Регистр фабрик компонентов
 * @author evga
 *
 */
public class ComponentFactoryRegister
    extends ObjectRegistr<IComponentFactory>
    implements IComponentFactory
{
    public IComponent createComponent(String id, Map<String, String> atts)
    {
        CreateComponentAcceptor acceptor = new CreateComponentAcceptor(id, atts);
        accept(acceptor);
        return acceptor.getComponent();
    }

    private class CreateComponentAcceptor
        implements IAcceptor<IComponentFactory>
    {
        private String id;
        private Map<String, String> atts;
        private IComponent component;

        public CreateComponentAcceptor(String id, Map<String, String> atts)
        {
            this.id = id;
            this.atts = atts;
        }

        public boolean accept(IComponentFactory factory)
        {
            IComponent c = factory.createComponent(id, atts);
            if (c != null)
            {
                this.component = c;
                return false;
            }
            return true;
        }

        public IComponent getComponent()
        {
            return component;
        }
    }
}
